package com.youth.orderservice.mq.Consumers;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rabbitmq.client.Channel;
import com.youth.orderservice.entity.TOrder;
import com.youth.orderservice.mq.entity.info.CreateOrderSimplInfo;
import com.youth.orderservice.service.TOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Slf4j
@Component
public class createOrderConsumer {

    @Autowired
    private TOrderService tOrderService;

    public static final String D_QUEUE = "QD";

    @RabbitListener(queues = D_QUEUE)
    public void receiveA(Message message, Channel channel){
        String s = new String(message.getBody());
        CreateOrderSimplInfo createOrderSimplInfo = JSON.parseObject(s, CreateOrderSimplInfo.class);
        /*获取订单id去查询订单状态*/
        String orderId = createOrderSimplInfo.getOrderId();
        TOrder tOrder = tOrderService.getOne(new QueryWrapper<TOrder>().eq("order_no", orderId));
        if (!StringUtils.isEmpty(tOrder)){
            if (tOrder.getStatus()==0){
                /*说明订单没有支付，删除订单*/
                tOrderService.deleteOrder(tOrder.getOrderId());
            }
        }
    }

}